function itemTypeChange(){
	if($('ItemReturns').value=='0'){
		$('ChoiceForm').show();
	}
	else{
		$('ChoiceForm').hide();
	}
	if($('ItemReturns').value=='3'){
		$('prompt').hide();
	}
	else{
		$('prompt').show();
	}
	if($('ItemReturns').value=='4'){
		$('NextButton').setAttribute('value', 'Continue');
		$('DoneButton').hide();
	}
	else{
		$('NextButton').setAttribute('value', 'Another Item');
		$('DoneButton').show();
	}
}

function addChoice(){
	var index = parseInt($('NumChoices').value);
	$('NumChoices').setAttribute('value', index+1);
	newField = $('Choices[0]').cloneNode(true);
	newField.setAttribute('id', 'Choices['+index+']');
	newField.setAttribute('name', 'choices['+index+']');
	$('ChoiceFormChoices').appendChild(document.createTextNode('Choice '+(index+1)+': '));
	$('ChoiceFormChoices').appendChild(newField);
	$('ChoiceFormChoices').appendChild(document.createElement('br'));
}

function makeSelect(optArray, moniker, index){
	var select = document.createElement("select");
	select.setAttribute('name', "alarms["+index+"]["+moniker+"]");
	select.setAttribute('id', "alarms["+index+"]["+moniker+"]");
	
	var opt = document.createElement("option");
	opt.setAttribute('value', '*');
	opt.appendChild(document.createTextNode('Every'));
	select.appendChild(opt);
	
	for(var i=0; i<optArray.size(); ++i){
		var opt = document.createElement("option");
		opt.setAttribute('value', i);
		opt.appendChild(document.createTextNode(optArray[i]));
		select.appendChild(opt);
	}
	return select;
}

function hackArray(start, end){
	rvar = [];
	for(var i=start; i<=end; ++i){
		rvar[i-start] = i;
	}
	return rvar;
}

function addAlarm(){
	var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
	var days = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'].concat(hackArray(1,31));
	var hours = hackArray(0, 23);
	var minutes = hackArray(0,59);

	var index = parseInt($('numAlarms').value);
	$('numAlarms').setAttribute('value', index+1);
	
	$('alarms').appendChild(document.createTextNode("Set alarm for - Month:"));
	$('alarms').appendChild(makeSelect(months, "month", index));
	$('alarms').appendChild(document.createTextNode("Day:"));
	$('alarms').appendChild(makeSelect(days, "day", index));
	$('alarms').appendChild(document.createTextNode("Hour:"));
	$('alarms').appendChild(makeSelect(hours, "hour", index));
	$('alarms').appendChild(document.createTextNode("Minute:"));
	$('alarms').appendChild(makeSelect(minutes, "minute", index));
	$('alarms').appendChild(document.createElement('br'));
}

//code borrowed from http://blog.firetree.net/2005/07/04/javascript-find-position/
function findPosX(obj){
	var curleft = 0;
	if(obj.offsetParent)
		while(1) 
		{
		  curleft += obj.offsetLeft;
		  if(!obj.offsetParent)
		    break;
		  obj = obj.offsetParent;
		}
	else if(obj.x)
		curleft += obj.x;
	return curleft;
}

//code borrowed from http://blog.firetree.net/2005/07/04/javascript-find-position/
function findPosY(obj){
	var curtop = 0;
	if(obj.offsetParent)
		while(1)
		{
		  curtop += obj.offsetTop;
		  if(!obj.offsetParent)
		    break;
		  obj = obj.offsetParent;
		}
	else if(obj.y)
		curtop += obj.y;
	return curtop;
}

function onPicture(x, y){
	picLeft = findPosX($('magicPic'));
	picTop = findPosY($('magicPic'));
	picRight = picLeft + $('magicPic').width
	picBottom = picTop + $('magicPic').height
	
	return (x>=picLeft && y>=picTop && x<=picRight && y<=picBottom);
}

function startRec(event){
	var x = event.clientX - findPosX($('magicPic'));
	var y = event.clientY - findPosY($('magicPic'));
		$('startx').setAttribute('value', x);
		$('starty').setAttribute('value', y);
}

function makeRecInput(type, field, val){
	var index = $('index').value
	var tag = document.createElement('input');
	tag.setAttribute('type', type);
	tag.setAttribute('name', 'data['+index+'][Rectangle]['+field+']');
	tag.setAttribute('id', 'data['+index+'][Rectangle]['+field+']');
	tag.setAttribute('value', val);
	
	return tag;
}

function closeRec(event, item_id){
	var x = event.clientX - findPosX($('magicPic'));
	var y = event.clientY - findPosY($('magicPic'));
	//if(x>=0 && y>=0 && x<=$('magicPic').width && y<=$('magicPic').height && $('startx').value!='-1'){
		var pointx = $('startx').value;
		var pointy = $('starty').value;
		var width = x - pointx;
		var height = y - pointy;
		
		$('recs').appendChild(document.createTextNode("("+pointx+", "+pointy+") - ("+x+", "+y+"): "));
		$('recs').appendChild(makeRecInput('hidden', 'pointx', pointx));
		$('recs').appendChild(makeRecInput('hidden', 'pointy', pointy));
		$('recs').appendChild(makeRecInput('hidden', 'width', width));
		$('recs').appendChild(makeRecInput('hidden', 'height', height));
		$('recs').appendChild(makeRecInput('hidden', 'pic_width', $('magicPic').width));
		$('recs').appendChild(makeRecInput('hidden', 'pic_height', $('magicPic').height));
		$('recs').appendChild(makeRecInput('hidden', 'item_id', item_id));
		$('recs').appendChild(makeRecInput('text', 'name', ''));
		$('recs').appendChild(document.createElement('br'));
		$('index').setAttribute('value', parseInt($('index').value)+1);
		
		$('startx').setAttribute('value', '-1');
		$('starty').setAttribute('value', '-1');
}

function setRec(event, item_id){
	if($('startx').value=='-1'){
		startRec(event);
		$('highlight').show();
		$('highlight').style.left = event.clientX+'px';
		$('highlight').style.top = event.clientY+'px';
		$('highlight').style.width = '0px';
		$('highlight').style.height = '0px';
		window.hstartx = event.clientX;
		window.hstarty = event.clientY;
	}
	else{
		closeRec(event, item_id);
		//$('highlight').hide();
	}
}

function drawHighlight(event){
	if($('startx').value!=-1){
		//var w = event.clientX - $('startx').value - findPosX($('magicPic'));
		//var h = event.clientY - $('starty').value - findPosY($('magicPic'));
		var w = event.clientX - window.hstartx;
		var h = event.clientY - window.hstarty;
		if(w<0){
			$('highlight').style.left = event.clientX+'px';
			w = -w;			
		}
		if(h<0){
			$('highlight').style.top = event.clientY+'px';
			h = -h;
		}
		$('highlight').style.width = w+'px';
		$('highlight').style.height = h+'px';
	}
}
